package whut.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.dom4j.DocumentException;

import util.Constants;
import util.ReadConfig;
import util.Tools;
import whut.model.Grbt;
import whut.model.Mgrd;
import whut.model.Ocs;
import whut.model.ShellStress;
import whut.model.Sshps;
import whut.service.GrbtService;
import whut.service.MgrdService;
import whut.service.OcsService;
import whut.service.ShellStressService;
import whut.service.SshpsService;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;


public class RealTimeAction {
	
	/** -------------Object Injection-------------- */
	private MgrdService mgrdService;
	public MgrdService getMgrdService() {
		return mgrdService;
	}
	public void setMgrdService(MgrdService mgrdService) {
		this.mgrdService = mgrdService;
	}
	
	GrbtService grbtService;
	public GrbtService getGrbtService() {
		return grbtService;
	}
	public void setGrbtService(GrbtService grbtService) {
		this.grbtService = grbtService;
	}
	private ShellStressService shellStressService;
	public ShellStressService getShellStressService() {
		return shellStressService;
	}
	public void setShellStressService(ShellStressService shellStressService) {
		this.shellStressService = shellStressService;
	}
	
	private OcsService ocsService;

	public OcsService getOcsService() {
		return ocsService;
	}
	public void setOcsService(OcsService ocsService) {
		this.ocsService = ocsService;
	}
	
	private SshpsService sshpsService;
	public SshpsService getSshpsService() {
		return sshpsService;
	}
	public void setSshpsService(SshpsService sshpsService) {
		this.sshpsService = sshpsService;
	}
	/** -------------Object Injection-------------- */
	
	/** @throws DocumentException 
	 * @throws NumberFormatException */
	public void Realtime() throws IOException, NumberFormatException, DocumentException
	{
		Map<String, Object> data = new HashMap<String, Object>();
		Mgrd mgrd;
		Grbt grbt;
		Ocs ocs;
		ShellStress shellStress ;
		Sshps sshps = new Sshps();
		/** ----------------------磨盘磨辊接近位移--------------------------- */
		if(mgrdService.getMGRD() != null)
		{
			mgrd = mgrdService.getMGRD();
			data.put("time", mgrd.getCd_time());
			/** ----------------------num1&&position1--------------------------- */
			if(null==mgrd.getCd_num1())
			{
				mgrd.setCd_num1("0");
			}
			int mgrd1level = Tools.judge4Points(Double.parseDouble(mgrd.getCd_num1()),"mgrd1");
			data.put("mgrd1", mgrd.getCd_num1());
			data.put("mgrd1level", Constants.swapLevel(mgrd1level));
		
			/** ----------------------num1&&position2--------------------------- */
			if(null==mgrd.getCd_num2())
			{
				mgrd.setCd_num2("0");
			}
			int mgrd2level = Tools.judge4Points(Double.parseDouble(mgrd.getCd_num2()),"mgrd2");
			data.put("mgrd2", mgrd.getCd_num2());
			data.put("mgrd2level", Constants.swapLevel(mgrd2level));
		}
		else
		{
			data.put("time", "0");
			data.put("mgrd1", "0");
			data.put("mgrd1level", "0");
			data.put("mgrd2", "0");
			data.put("mgrd2level", "0");
		}
		
		/** ----------------------摇臂液压缸压力--------------------------- */
		if(ocsService.getOcs() != null)
		{
			ocs = ocsService.getOcs();
			data.put("time", ocs.getHp_time());
			/** ----------------------num1&&position1--------------------------- */

			int ocs1level = Tools.judge4Points(ocs.getHp_num1().doubleValue(),"ocs1");
			data.put("ocs1", ocs.getHp_num1());
			data.put("ocs1level", Constants.swapLevel(ocs1level));
		
			/** ----------------------num1&&position2--------------------------- */
			int ocs2level= Tools.judge4Points(ocs.getHp_num2().doubleValue(),"ocs2");
			data.put("ocs2", ocs.getHp_num2());
			data.put("ocs2level", Constants.swapLevel(ocs2level));
			/** ----------------------num1&&position3--------------------------- */
			int ocs3level= Tools.judge4Points(ocs.getHp_num3().doubleValue(),"ocs3");
			data.put("ocs3", ocs.getHp_num3());
			data.put("ocs3level", Constants.swapLevel(ocs3level));
			/** ----------------------num1&&position4--------------------------- */
			int ocs4level= Tools.judge4Points(ocs.getHp_num4().doubleValue(),"ocs4");
			data.put("ocs4", ocs.getHp_num4());
			data.put("ocs4level", Constants.swapLevel(ocs4level));
		
		}
		else
		{
			data.put("time", "0");
			data.put("ocs1", "0");
			data.put("ocs1level", "0");
			data.put("ocs2", "0");
			data.put("ocs2level", "0");
			data.put("ocs3", "0");
			data.put("ocs3level", "0");
			data.put("ocs4", "0");
			data.put("ocs4level", "0");
		}
		/** ----------------------壳体螺栓应力--------------------------- */
		
		if(shellStressService.ShellStressfind() != null){
			shellStress = shellStressService.ShellStressfind();
			int hbs1level = Tools.judge2Points(shellStress.getSs_num1().doubleValue(),"hbs1");
			int hbs2level = Tools.judge2Points(shellStress.getSs_num2().doubleValue(),"hbs2");
			int hbs3level = Tools.judge2Points(shellStress.getSs_num3().doubleValue(),"hbs3");
			int hbs4level = Tools.judge2Points(shellStress.getSs_num4().doubleValue(),"hbs4");
			int hbs5level = Tools.judge2Points(shellStress.getSs_num5().doubleValue(),"hbs5");
			int hbs6level = Tools.judge2Points(shellStress.getSs_num6().doubleValue(),"hbs6");
			int hbs7level = Tools.judge2Points(shellStress.getSs_num7().doubleValue(),"hbs7");
			int hbs8level = Tools.judge2Points(shellStress.getSs_num8().doubleValue(),"hbs8");
		data.put("time", shellStress.getSs_time());
		data.put("hbs1", shellStress.getSs_num1());
		data.put("hbs1level", Constants.swapLevel(hbs1level));
		data.put("hbs2", shellStress.getSs_num2());
		data.put("hbs2level", Constants.swapLevel(hbs2level));
		data.put("hbs3", shellStress.getSs_num3());
		data.put("hbs3level", Constants.swapLevel(hbs3level));
		data.put("hbs4", shellStress.getSs_num4());
		data.put("hbs4level", Constants.swapLevel(hbs4level));
		data.put("hbs5", shellStress.getSs_num5());
		data.put("hbs5level", Constants.swapLevel(hbs5level));
		data.put("hbs6", shellStress.getSs_num6());
		data.put("hbs6level", Constants.swapLevel(hbs6level));
		data.put("hbs7", shellStress.getSs_num7());
		data.put("hbs7level", Constants.swapLevel(hbs7level));
		data.put("hbs8", shellStress.getSs_num8());
		data.put("hbs8level", Constants.swapLevel(hbs8level));
		
		}
		else{
			data.put("time", "0");
			data.put("hbs1", "0");
			data.put("hbs1level", "0");
			data.put("hbs2", "0");
			data.put("hbs2level", "0");
			data.put("hbs3", "0");
			data.put("hbs3level", "0");
			data.put("hbs4", "0");
			data.put("hbs4level", "0");
			data.put("hbs5", "0");
			data.put("hbs5level", "0");
			data.put("hbs6", "0");
			data.put("hbs6level", "0");
			data.put("hbs7", "0");
			data.put("hbs7level", "0");
			data.put("hbs8", "0");
			data.put("hbs8level", "0");
			
		}
		
		/** ----------------------摇臂销轴应力--------------------------- */
		if(sshpsService.getSSHPS() !=null)
		{
			sshps = (Sshps) sshpsService.getSSHPS();
			System.out.println("time is "+sshps.getPs_time());
			data.put("time",sshps.getPs_time());
			/** ----------------------num&&position---------------------------    */			
			Tools.data_put(Double.parseDouble(sshps.getPs_num1()),"sshps1","sshpslevel1",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num2()),"sshps2","sshpslevel2",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num3()),"sshps3","sshpslevel3",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num4()),"sshps4","sshpslevel4",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num5()),"sshps5","sshpslevel5",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num6()),"sshps6","sshpslevel6",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num7()),"sshps7","sshpslevel7",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num8()),"sshps8","sshpslevel8",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num9()),"sshps9","sshpslevel9",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num10()),"sshps10","sshpslevel10",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num11()),"sshps11","sshpslevel11",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num12()),"sshps12","sshpslevel12",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num13()),"sshps13","sshpslevel13",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num14()),"sshps14","sshpslevel14",data);
			Tools.data_put(Double.parseDouble(sshps.getPs_num15()),"sshps15","sshpslevel15",data);	
			/** ----------------------num&&position--------------------------- */
		}
		else
		{
			data.put("time", "0");
			data.put("sshps1", "0");
			data.put("sshpslevel1", "0");
			data.put("sshps2", "0");
			data.put("sshpslevel2", "0");
			data.put("sshps3", "0");
			data.put("sshpslevel3", "0");
			data.put("sshps4", "0");
			data.put("sshpslevel4", "0");
			data.put("sshps5", "0");
			data.put("sshpslevel5", "0");
			data.put("sshps6", "0");
			data.put("sshpslevel6", "0");
			data.put("sshps7", "0");
			data.put("sshpslevel7", "0");
			data.put("sshps8", "0");
			data.put("sshpslevel8", "0");
			data.put("sshps9", "0");
			data.put("sshpslevel9", "0");
			data.put("sshps10", "0");
			data.put("sshpslevel10", "0");
			data.put("sshps11", "0");
			data.put("sshpslevel11", "0");
			data.put("sshps12", "0");
			data.put("sshpslevel12", "0");
			data.put("sshps13", "0");
			data.put("sshpslevel13", "0");
			data.put("sshps14", "0");
			data.put("sshpslevel14", "0");
			data.put("sshps15", "0");
			data.put("sshpslevel15", "0");

		}
		// ReadConfig.read()方法得到阈值
		Map<String,String> map = ReadConfig.read();
		if(map!=null)
		{
			Iterator it = map.entrySet().iterator();
			while(it.hasNext())
			{
				Map.Entry entry = (Map.Entry) it.next();
				data.put((String) entry.getKey(),entry.getValue());
			}
		}
		
		ActionContext context = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) context
				.get(ServletActionContext.HTTP_RESPONSE);
		Gson gson = new Gson();
		String json = gson.toJson(data);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(json);
		response.getWriter().flush();
		response.getWriter().close();
	}
}
